<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>大河网订阅管理系统</title>
    
    <link rel="stylesheet"  href="css/style.css" type="text/css" media="all" />
	<script src="js/My97DatePicker/WdatePicker.js"
			type="text/javascript"></script>
	<script src="js/jQuery-3.7.1.js"></script>
	<script src="js/tools.js"></script>
	<style>
	</style>

  </head>
  
  <body class="ContentBody"> 
		<form action=""id="form-edit">
			<div class="MainDiv">
				<table width="99%" border="0" cellpadding="0" cellspacing="0" class="CContent">
					<tr>
						<th class="tablestyle_title" >文章编辑</th>
					</tr>
					<tr>
						<td class="CPanel">
							<table border="0" cellpadding="0" cellspacing="0" style="width:100%">
								<tr>
									<td width="100%">
										<fieldset style="height:100%;"><legend>添加编辑</legend>
											<table border="0" cellpadding="2" cellspacing="1" style="width:100%" id="mytab">
												<tr>
													<td nowrap align="right">日期:	</td>
													<td><input name="newsDate" id="newsDate" style="width:154px" class="Wdate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" />
													<font color="red"> *</font></td>
													<td align="right" width="19%">报纸名称:</td>
													<td width="27%"><input name="newsName" id="newsName" class="text" style="width:154px" />
													<font color="red"> *</font></td>
												</tr>
												 <tr>
													<td nowrap align="right">版名:</td>
													<td><input name="layoutName" id="layoutName" class="text" style="width:154px" />
													<font color="red"> *</font></td>
													<td nowrap align="right">文章作者:</td>
													<td><input name="newsAuthor" id="newsAuthor" class="text" style="width:154px" /></td>
												</tr>
												<tr>
													<td nowrap align="right">文章标题:</td>
													<td colspan="3"><input name="newsTitle" id="newsTitle" class="text" style="width:354px" /></td>
												</tr>
												<tr>
													<td nowrap align="right">文章简介:</td>
													<td colspan="3">
													<textarea id="newsInfo" style="width:600px;height:50px" name="newsInfo"></textarea>
													</td>
												</tr>
												<tr>
													<td nowrap align="right">内容:</td>
													<td colspan="3">
														<textarea id="newsContent" style="width:600px;height:400px;" name="newsContent"></textarea>
														<input type="hidden" id="id" name="id">
													</td>
												</tr>
											</table>
										</fieldset>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td colspan="2" align="center" height="50px">
							<input type="button" name="Submit" value="保存" class="button" id="btn-edit"/>
							<input type="button" name="Submit2" value="返回" class="button" onclick="window.history.go(-1);"/>
						</td>
					</tr>
				</table>
			</div>
	    </form>
		<script>
			// 页面加载完成,获取url中的id,从服务器获取新闻信息
			window.onload = function () {
				// 1.获取id
				let id = getQueryString("id");
				// 2.发送请求.获取新闻信息 第一个news指的是访问路径
				$.getJSON("news/getNewsById",{"id":id},function (result) {
					// 判断返回结果
					if (result.code == 200){
						$("#newsDate").val(result.data.newsDate)
						$("#newsName").val(result.data.newsName)
						$("#layoutName").val(result.data.layoutName)
						$("#newsAuthor").val(result.data.newsAuthor)
						$("#newsTitle").val(result.data.newsTitle)
						$("#newsInfo").val(result.data.newsInfo)
						$("#newsContent").val(result.data.newsContent)
						// 在此处为隐藏域的id赋值
						$("#id").val(result.data.id)
					}else {
						alert("数据加载失败,请刷新后重试!")
					}
				})
			}

			// 保存按钮添加点击事件
			$("#btn-edit").click(function () {
				// 1.获取表单数据作为请求参数
				let sendData = $("#form-edit").serialize();
				// 2.发送请求
				$.getJSON("news/edit",sendData,function (result) {
					if (result.code == 200){
						// 修改成功
						alert("修改成功!")
						// 跳转返回main.html
						window.location.href = "main.html"
					}else {
						// 修改失败
						alert("修改失败,请检查后重试!")
					}
				})
			})
		</script>
  </body>
</html>